package com.telit.firecommand.controller;


import com.telit.common.constant.Constant;
import com.telit.common.entity.DataReturn;
import com.telit.common.enums.ResultCode;
import com.telit.common.util.OssUtils;
import com.telit.common.vo.FileVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * @author chencf
 * @description 多媒体上传接口
 */
@RestController
@RequestMapping("/media-upload")
@Api(tags = "多媒体上传接口", description = "/media-upload")
public class MediaUploadController {


    @Value("${file.bucket:public}")
    private String bucket;

    @PostMapping(value = "/uploadFile", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "上传文件单个并返回文件信息", httpMethod = Constant.REQUEST_METHOD_POST)
    public DataReturn<FileVO> uploadFile(MultipartFile file) {
        FileVO result = OssUtils.upload(file, bucket, null);
        return DataReturn.ok(result);

    }

    @PostMapping(value = "/uploadFiles", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "批量上传并返回文件信息", httpMethod = Constant.REQUEST_METHOD_POST)
    public DataReturn uploadFiles(@RequestParam(value = "files") List<MultipartFile> files) {
        if (CollectionUtils.isEmpty(files)) {
            return DataReturn.failure(ResultCode.REQUEST_PARAMS_CHECK_ERROR, "上传文件不能为空");
        }
        List<FileVO> uploads = OssUtils.uploads(files, bucket);
        // 返回图片地址
        return DataReturn.ok(uploads);
    }
}
